package light.mvc.controller.item;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.page.base.Grid;
import light.mvc.page.base.Json;
import light.mvc.page.base.PageFilter;
import light.mvc.page.base.SessionInfo;
import light.mvc.page.base.Status;
import light.mvc.page.item.Treatment;
import light.mvc.service.item.TreatmentServiceI;
import light.mvc.utils.GlobalConstant;

import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/treatment")
public class TreatmentController extends BaseController {

	@Autowired
	private TreatmentServiceI treatmentService;
	
	@RequestMapping("/manager")
	public String manager() {
		return "/item/treatment";
	}
	
	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(Treatment treatment, PageFilter ph) {
		Grid grid = new Grid();
		grid.setRows(treatmentService.dataGrid(treatment, ph));
		grid.setTotal(treatmentService.count(treatment, ph));
		return grid;
	}
	
	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest request) {
		return "/item/treatmentAdd";
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Json add(Treatment treatment) {
		String errorMsg = null;
        int errorCode = -1;
        Json j = null;
		if(treatmentService.getByItem(treatment)!=null){
			errorMsg = "治疗项目已存在!";
			j = new Json(new Status(errorCode,errorMsg),null);
		}else{
			try {
				treatmentService.add(treatment);
				errorCode = 0;
				errorMsg = "增加成功";
				j = new Json(new Status(errorCode,errorMsg),treatmentService.getByItem(treatment));
			} catch (Exception e) {
				errorMsg = e.getMessage();
				j = new Json(new Status(errorCode,errorMsg),null);
			}
			
		}
        return j;
	}
	
	@RequestMapping("/get")
	@ResponseBody
	public Json get(HttpServletRequest request) {
		String errorMsg = "信息获取失败";
        int errorCode = -1;
        
        SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_INFO);
        Integer id = sessionInfo.getId();
		Treatment treatment = treatmentService.get(id);
		if(treatment != null) {
			errorMsg = "信息获取成功！";
			errorCode = 0;
		}
        
		Json j = new Json(new Status(errorCode, errorMsg),treatment);
		return j;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(Integer id) {
		String errorMsg = null;
        int errorCode = -1;
		try {
			treatmentService.delete(id);
			errorMsg = "删除成功！";
			errorCode = 0;
		} catch (Exception e) {
			errorMsg = e.getMessage();
		}
		Json j = new Json(new Status(errorCode,errorMsg),null);
        return j;
	}
	
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, Integer id) {
		Treatment treatment = treatmentService.get(id);
		request.setAttribute("treatment", treatment);
		return "/item/treatmentEdit";
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(HttpServletRequest request,Treatment treatment) {
		String errorMsg = null;
        int errorCode = -1;
       
		try {
			treatmentService.edit(treatment);
			errorCode = 0;
			errorMsg = "编辑成功！";
		} catch (ServiceException e) {
			errorMsg = "编辑失败:" + e.getMessage();
		}
		Json j = new Json(new Status(errorCode,errorMsg),null);
        return j;
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public List<Treatment> list() {
		return treatmentService.list();
	}

}
